<?php

namespace app\api\model\kuyou\applet;

use app\api\controller\kuyou\applet\Commons;
use think\Model;
use think\Db;
use Exception;
class StaffMember extends Model
{

    // 表名
    protected $name = 'kuyou_staff_member';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    /**
     * 获取列表
     * @param array $where
     * @param int $page
     * @param int $limit
     * @return array
     */
    public static function getList($where,$page,$limit){
        $app_page = config('app_page');
        $app_page = $app_page===false?false:true;
        $pageStatus = true;
        if($app_page){
            if($page<0){
                $pageStatus = false;
            }
        }
        if($pageStatus){
            $list = collection(self::where(function ($query) use ($where) {
                $query->where($where);
            })->page($page,$limit)->field("id,men_code,name,phone,status,FROM_UNIXTIME(createtime,'%Y-%m-%d %H:%i:%s') as createtime")->select())->toArray();
        }else{
            $list = collection(self::where(function ($query) use ($where) {
                $query->where($where);
            })->field("id,men_code,name,phone,status,FROM_UNIXTIME(createtime,'%Y-%m-%d %H:%i:%s') as createtime")->select())->toArray();
        }

        $count = self::where(function ($query) use ($where) {
            $query->where($where);
        })->count();
        return ['list'=>$list,'count'=>$count];
    }

    /**
     * 获取详情
     * @param int $id
     * @return array
     */
    public static function getDetail($id){
        $detail = self::where(function ($query) use ($id) {
            $query->where('id', '=', $id);
        })->field("id,men_code,name,phone,status,FROM_UNIXTIME(createtime,'%Y-%m-%d %H:%i:%s') as createtime")->find();
        return $detail;
    }

    /**
     * 添加
     * @param array $detail
     * @return array
     */
    public static function addDetail($detail){
        if(empty($detail)){
            return ['status'=>400,'msg'=>'参数错误'];
        }else{
            $check = (new Commons())->checkParameter($detail,[
                'men_code|编号'=>'require',
                'name|名称'=>'require',
                'status|状态'=>'require|in:0,1',
            ]);
            if(isset($check['status'])){
                return $check;
            }else{
                $status = (new Commons())->checkCode($detail['men_code'],3);//验证编号是否合法
                if($status){
                    $model = new StaffMember();
                    $row = $model->where(['men_code'=>$detail['men_code']])->value('id');
                    if($row){
                        return ['status'=>500,'msg'=>'编号已存在'];
                    }
                    $res = false;
                    Db::startTrans();
                    try {
                        $res = $model->allowField(true)->save($detail);
                        Db::commit();
                    } catch (Exception $e) {
                        Db::rollback();
                    }

                    if($res){
                        return ['status'=>200,'msg'=>$model->id];
                    }else{
                        return ['status'=>500,'msg'=>'添加失败'];
                    }
                }else{
                    return ['status'=>500,'msg'=>'编号规则错误'];
                }
            }
        }
    }

    /**
     * 更新
     * @param array $detail
     * @return array
     */
    public static function editDetail($detail){
        if(empty($detail)){
            return ['status'=>400,'msg'=>'参数错误'];
        }else{
            $check = (new Commons())->checkParameter($detail,[
                'id|ID'=>'require|number',
                'men_code|编号'=>'require',
                'name|名称'=>'require',
                'status|状态'=>'require|in:0,1',
            ]);
            if(isset($check['status'])){
                return $check;
            }else{
                $status = (new Commons())->checkCode($detail['men_code'],3);//验证编号是否合法
                if($status){
                    $model = new StaffMember();

                    $row = $model->where(['men_code'=>$detail['men_code'],'id'=>['<>',$detail['id']]])->value('id');
                    if($row){
                        return ['status'=>500,'msg'=>'编号已存在'];
                    }

                    $row = $model->get($detail['id']);
                    if($row){
                        $res = false;
                        Db::startTrans();
                        try {
                            $res = $row->allowField(true)->save($detail);
                            Db::commit();
                        } catch (Exception $e) {
                            Db::rollback();
                        }
                        if($res){
                            return ['status'=>200,'msg'=>'更新成功'];
                        }else{
                            return ['status'=>500,'msg'=>'更新失败'];
                        }
                    }else{
                        return ['status'=>500,'msg'=>'未查询到可更新数据'];
                    }
                }else{
                    return ['status'=>500,'msg'=>'编号规则错误'];
                }
            }
        }
    }

    /**
     * 删除
     * @param int $id
     * @return array
     */
    public static function delDetail($id){
        if(empty($id)){
            return ['status'=>400,'msg'=>'参数错误'];
        }else{
            $res = (new StaffMember())->where(['id'=>['in',$id]])->delete();
            if($res){
                return ['status'=>200,'msg'=>$res];
            }else{
                return ['status'=>500,'msg'=>'删除失败'];
            }
        }
    }

    /**
     * 导入
     * @param array $add,$edit
     * @return array
     */
    public static function import($add,$edit){
        if(empty($add) && empty($edit)){
            return ['status'=>400,'msg'=>'参数错误'];
        }else{
            if(!empty($add)){
                $res = (new StaffMember())->insertAll($add);
            }
            if(!empty($edit)){
                foreach ($edit as $edit_k=>$edit_v){
                    $res = (new StaffMember())->where(['id'=>$edit_v['id']])->update($edit_v);
                }
            }
            if($res){
                return ['status'=>200,'msg'=>'导入成功'];
            }else{
                return ['status'=>500,'msg'=>'导入失败'];
            }
        }
    }
}
